package com.yq.servlet;

import org.w3c.dom.html.HTMLObjectElement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@WebServlet("/servletTeacher")
public class ServletTeacher extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        //获取作业提交的目录
        ArrayList<String> names = new ArrayList<>();
        ArrayList<String> allName = new ArrayList<>();
        allName.add("张三");
        allName.add("李四");
        allName.add("王五");
        // String homeworkdir = (String) getServletContext().getAttribute("homeworkdir");
        String date = new SimpleDateFormat("MM-dd").format(new Date());
        String homeworkdir = getServletContext().getRealPath("homework/"+date);
//        if (homeworkdir==null){
//            response.getWriter().print("还没有学生提交作业");
//            response.setHeader("refresh","3;url=/file/");
//            return;
//        }
        File file = new File(homeworkdir);
        if (file.isDirectory()){
            String[] list = file.list();
            if (list!=null && list.length!=0){
                for (String s : list) {
                    String[] s1 = s.split("_");
                    String[] s2 = s1[s1.length - 1].split("\\.");
                    names.add(s2[0]);
                }
                response.getWriter().println("已经提交的学生"+names+"<br>");
            }

        }
        allName.removeAll(names);
        response.getWriter().println("未提交的学生"+allName);

        //获取所有文件名
        //提取出提交学生的姓名
        //与全班的学生进行比对
        //输出结果到控制台
    }
}
